package com.jiantong.work.impl.dao;

import com.google.common.collect.Maps;
import com.jiantong.work.model.ButcherReport;
import com.jiantong.work.model.ButcherReportSurveyRecord;
import io.terminus.common.model.Paging;
import io.terminus.common.mysql.dao.MyBatisDao;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by niufei on 2017/3/22.
 *
 */
@Repository
public class ButcherReportDao extends MyBatisDao<ButcherReport>{

    public Integer findStatusById(Long id){
        return getSqlSession().selectOne(sqlId("findStatusById"),id);
    }

    /**
     * 返回当前单据的编码的最大值
     * @param number 除去流水号的前缀
     * @return string
     */
    public String findCurrentMaxNumber(String number){
        return getSqlSession().selectOne(sqlId("findCurrentMaxNumber"),number);
    }

    public Paging<ButcherReport> pagingButcherReportAll(Integer offset , Integer limit, Map<String ,Object> criteria){
        if(criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = (Long)getSqlSession().selectOne(this.sqlId("countAll"), criteria);
        if(total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<ButcherReport> data = getSqlSession().selectList(this.sqlId("pagingAll"), criteria);
            return new Paging<>(total, data);
        }
    }

    public Paging<ButcherReport> pagingCommon(Integer offset , Integer limit, Map<String ,Object> criteria){
        if(criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = (Long)getSqlSession().selectOne(this.sqlId("count1"), criteria);
        if(total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<ButcherReport> data = getSqlSession().selectList(this.sqlId("paging1"), criteria);
            return new Paging<>(total, data);
        }
    }

    public Paging<ButcherReport> pagingCert(Integer offset , Integer limit, Map<String ,Object> criteria) {
        if (criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = (Long) getSqlSession().selectOne(this.sqlId("certcount"), criteria);
        if (total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<ButcherReport> data = getSqlSession().selectList(this.sqlId("cert"), criteria);
            return new Paging<>(total, data);
        }

    }


}
